<?php 
defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
class Controller_purchases_paydetail extends Controller_Admin{

 /**
 *采购付款单查询
 *
 */
    public function before()
    {
    	try {
    		  ACL::Required('select purchase paydetail');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
				
        parent::before();
    }

	/*
  		查询采购付款单的详细信息
  	*/
  public function action_paydetail() {
  		$this->title='采购付款明细报表';//获取值时候 方式有问题
  		$pay_detail = DB::select()->from('purchase_pays');

  		if(isset($_GET['date_from'])) {
  			$pay_detail->where('pay_date','>=',$_GET['date_from'].' 00:00:00');
  			
  		}
  		if(isset($_GET['date_to']))
			{
				$pay_detail->where('pay_date','<=',$_GET['date_to'].' 23:59:59');
			}
  	
  		if(isset($_GET['purchaseorder_id'])) 
			{
  			$pay_detail->where('purchaseorder_id','=',$_GET['purchaseorder_id']);
  		}
  		if(isset($_GET['supplier_id']))
			{
//                  ------------------------------------kdw----------------------------------
//					$supplier = DB::select('id')->from('erp_provider')
//													->where('vProviderName','=',$_GET['supplier_id'])
//													->execute('readonlyerp')
//													->as_array();
                    $fields=array('id',);
                    $supplier_name=$_GET['supplier_id'];
                    $conditions=array('name'=>"='".$supplier_name."'",);
                    $suppliers=Newerpclientapi::getSuppliers($fields,$conditions);
                    $supplier=array();
                    foreach($suppliers as $sup){
                        $supplier[]=$sup['id'];
                    }
//                  -------------------------------------kdw-end-----------------------------
					if(empty($supplier))$supplier=array(1);
  				$pay_detail->where('supplier_id','IN',$supplier);
  		}
			$pagination         = Pagination::factory(array(
	      'items_per_page'  =>20, 
	      'total_items'     => count($pay_detail->execute('newerp')->as_array()),
	      'uri'	            => $this->request->uri(),
	  	));
	  	$pay_detail       ->limit($pagination->items_per_page);
			$pay_detail       ->offset($pagination->_offset);
	  	$pay_details      = $pay_detail->execute('newerp')->as_array();	
  		$views= View::factory('purchase/paydetail');
  		$views->set('date_from',isset($_GET['date_from'])?$_GET['date_from']:'');
  		$views->set('date_to',isset($_GET['date_to'])?$_GET['date_to']:'');
  		$views->set('purchaseorder_id',isset($_GET['purchaseorder_id'])?$_GET['purchaseorder_id']:'');
  		$views->set('supplier_id',isset($_GET['supplier_id'])?$_GET['supplier_id']:'');
  		$views->set('pay_detail',$pay_details);
			$views->set('pagination',$pagination);

  		$this->response->body($views);
  	}  		
  }

?>
